// 汇率
export default class ExchangeRate {
    constructor(props) {
        this.CNY = props.CNY;
        this.RUB = props.RUB;
        this.USD = props.USD;
    }
    static subscriptionCallBack = null;

    calculate(type, prices) {
        let price = parseFloat(prices);
        const callBack = {
            CNY: null,
            RUB: null,
            USD: null
        }
        // 汇率换算
        switch (type) {
            case 'CNY':
                callBack.CNY = price;
                callBack.RUB = (price * this.RUB.value);
                callBack.USD = (price * this.USD.value);
                break;
            case 'RUB':
                callBack.RUB = price;
                callBack.CNY = (price / this.RUB.value);
                callBack.USD = (price * (this.USD.value / this.RUB.value));
                break;
            case 'USD':
                callBack.USD = price;
                callBack.RUB = (price * (this.RUB.value / this.USD.value));
                callBack.CNY = (price / this.USD.value);
                break;
            default:
                callBack.USD = 0;
                callBack.RUB = 0;
                callBack.CNY = 0;
                break
        }
        
        if (this.subscriptionCallBack) {
            this.subscriptionCallBack(callBack)
        }
        return callBack;
    };


    //订阅汇率
    subscriptionExchangeRate(callBack) {
        this.subscriptionCallBack = callBack;
    }

    //取消订阅
    unsubscribeExchangeRate() {
        this.subscriptionCallBack = null;
    }

}